import 'dart:async';
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';

/// 网络状态监听的 State 基类
abstract class BaseNetState<T extends StatefulWidget> extends State<T> {
  /// 网络状态订阅
  StreamSubscription<ConnectivityResult> _subscription;
  String netStatus;

  @override
  void initState() {
    // 添加网络状态的改变监听
    _subscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult result) {
      // 保存状态
      netStatus = result.toString().split('.')[1];
    });
    super.initState();
  }

  @override
  void dispose() {
    // 取消订阅
    _subscription.cancel();
    super.dispose();
  }
}
